import type { UploadFile } from 'ant-design-vue';

import { request } from './request';

export async function tdesignUploadFile(file: UploadFile) {
  try {
    const data = await request.Post('/upload', {
      ...file,
    });
    return { status: 'success', response: data };
  } catch (error: any) {
    console.error(error);
    return { status: 'fail', error: error.message };
  }
}

interface UploadFileParams {
  file: File;
  onError?: (error: Error) => void;
  onProgress?: (progress: { percent: number }) => void;
  onSuccess?: (data: any, file: File) => void;
}
export async function uploadFile({
  file,
  onError,
  onProgress,
  onSuccess,
}: UploadFileParams) {
  try {
    onProgress?.({ percent: 0 });

    const data = await request.Post('/upload', { file });

    onProgress?.({ percent: 100 });
    onSuccess?.(data, file);
  } catch (error) {
    onError?.(error instanceof Error ? error : new Error(String(error)));
  }
}
